/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ClientNew.java
 *
 * Created on 09-dic-2011, 16:38:23
 */
package edu.uoc.tdp.pac4.only5.gui.client;

import edu.uoc.tdp.pac4.only5.domain.EnumRole;
import edu.uoc.tdp.pac4.only5.gui.client.panels.connexio.ConsultaReservesPanel;
import edu.uoc.tdp.pac4.only5.gui.client.panels.connexio.HistoricDespesesPanel;
import edu.uoc.tdp.pac4.only5.gui.client.panels.manteniment.AulaPanel;
import edu.uoc.tdp.pac4.only5.gui.client.panels.manteniment.RecursPanel;
import edu.uoc.tdp.pac4.only5.gui.client.panels.manteniment.UsuariPanel;
import edu.uoc.tdp.pac4.only5.manager.BookingManager;
import edu.uoc.tdp.pac4.only5.manager.PaymentManager;
import edu.uoc.tdp.pac4.only5.manager.ResourceManager;
import edu.uoc.tdp.pac4.only5.manager.SpaceManager;
import edu.uoc.tdp.pac4.only5.manager.UserManager;
import edu.uoc.tdp.pac4.only5.manager.impl.DataBaseConnectionManagerImpl;
import edu.uoc.tdp.pac4.only5.manager.impl.PaymentManagerImpl;
import edu.uoc.tdp.pac4.only5.manager.impl.ResourceManagerImpl;
import edu.uoc.tdp.pac4.only5.manager.impl.SpaceManagerImpl;
import edu.uoc.tdp.pac4.only5.tools.ResourceBundleTool;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

/**
 *
 * @author Gerard
 */
public class ClientNew extends javax.swing.JFrame {
    
      /*
     * MANAGERS:
     */
    private BookingManager bookingManager;
    private PaymentManager paymentManager;
    private ResourceManager resourceManager;
    private SpaceManager spaceManager;
    private UserManager userManager;
    
    
    /*
     * PANELS PART CLIENT:
     */
    private ConsultaReservesPanel consultaReservesPanel;
    private HistoricDespesesPanel historicDespesesPanel;
    private edu.uoc.tdp.pac4.only5.gui.client.panels.connexio.ConsultaAulesPanel consultaAulesPanel;
    
    /*
     * PANELS PART AULES:
     */
    private edu.uoc.tdp.pac4.only5.gui.client.panels.aules.ConsultaAulesPanel consultaAulesPanelPartAules;
    
    /*
     * PANELS PART MANTENIMENT:
     */
    private UsuariPanel usuariPanel;
    private AulaPanel aulaPanel;
    private RecursPanel recursPanel;
    
    /*
     * I18N
     */
    private String getText(String key){
        return ResourceBundleTool.getInstance().getText(key);
    }
    
    /** Creates new form ClientNew */
    public ClientNew() {
        try {
            initComponents();
            this.setLayout(new FlowLayout());
            setMenuDependsOfRole(ClientSession.getInstance().getRole());
            bookingManager = (BookingManager) Naming.lookup("rmi://localhost/BookingManager");
            paymentManager = (PaymentManager) Naming.lookup("rmi://localhost/PaymentManager");
            spaceManager = (SpaceManager) Naming.lookup("rmi://localhost/SpaceManager");
            resourceManager = (ResourceManager) Naming.lookup("rmi://localhost/ResourceManager");
            userManager = (UserManager) Naming.lookup("rmi://localhost/UserManager");
            setTitle("eEspais - Only5");
        } catch (NotBoundException ex) {
            Logger.getLogger(ClientNew.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MalformedURLException ex) {
            Logger.getLogger(ClientNew.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RemoteException ex) {
            Logger.getLogger(ClientNew.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenuBar1 = new javax.swing.JMenuBar();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 299, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ClientNew.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ClientNew.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ClientNew.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ClientNew.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new ClientNew().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuBar jMenuBar1;
    // End of variables declaration//GEN-END:variables

    private void setMenuDependsOfRole(EnumRole role) {
        if (EnumRole.ADMINISTRADOR.equals(role)) {
            buildAdministradorMenu();
        } else if (EnumRole.CLIENT.equals(role)) {
            buildClientMenu();
        } else if (EnumRole.TECNIC.equals(role)) {
            buildTecnicMenu();
        }
    }

    private void buildAdministradorMenu() {
        JMenu administradorMenu = new JMenu();
        administradorMenu.setText(getText("mnu.administrador"));
        
        JMenuItem mnuUsuariPanel = new JMenuItem();
        mnuUsuariPanel.setText(getText("mnu.administrador.MantUsuari"));
          mnuUsuariPanel.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnu_administrador_nouUsuariActionPerformed(evt);
            }
        });
        administradorMenu.add(mnuUsuariPanel);
        
        JMenuItem mnuAulaPanel = new JMenuItem();
        mnuAulaPanel.setText(getText("mnu.administrador.MantAula"));
          mnuAulaPanel.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnu_administrador_novaAulaActionPerformed(evt);
            }
        });
        administradorMenu.add(mnuAulaPanel);
        
        JMenuItem mnuRecursPanel = new JMenuItem();
        mnuRecursPanel.setText(getText("mnu.administrador.MantRecurs"));
          mnuRecursPanel.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnu_administrador_consultaUsuarisActionPerformed(evt);
            }
        });
        administradorMenu.add(mnuRecursPanel);
        jMenuBar1.add(administradorMenu);
    }

    private void buildClientMenu() {
        JMenu clientMenu = new JMenu();
        clientMenu.setText(getText("mnu.client"));

        JMenuItem mnuConsultaReserves = new JMenuItem();
        mnuConsultaReserves.setText(getText("mnu.client.consultareserves"));
        mnuConsultaReserves.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnu_gestioreserves_consultaReservesActionPerformed(evt);
            }
        });
        clientMenu.add(mnuConsultaReserves);
        
        JMenuItem mnuReservarAula = new JMenuItem();
        mnuReservarAula.setText(getText("mnu.client.reservaraula"));
        mnuReservarAula.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnu_gestioreserves_novaReservaActionPerformed(evt);
            }
        });
        clientMenu.add(mnuReservarAula);
        
        JMenuItem mnuHistoric = new JMenuItem();
        mnuHistoric.setText(getText("mnu.client.historicaula"));
        mnuHistoric.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnu_gestioreserves_consultaHistoricActionPerformed(evt);
            }
        });
        clientMenu.add(mnuHistoric);
        jMenuBar1.add(clientMenu);
    }

    private void buildTecnicMenu() {
        JMenu tecnicMenu = new JMenu();
        tecnicMenu.setText(getText("mnu.tecnic"));
        
        JMenuItem mnuHistoric = new JMenuItem();
        mnuHistoric.setText(getText("mnu.tecnic.consultaaules"));
        mnuHistoric.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnu_tecncic_consultaAulesActionPerformed(evt);
            }
        });
        tecnicMenu.add(mnuHistoric);
        jMenuBar1.add(tecnicMenu);
    }

    private void mnu_gestioreserves_novaReservaActionPerformed(ActionEvent evt) {
        desactivarPanels();
        consultaAulesPanel = new edu.uoc.tdp.pac4.only5.gui.client.panels.connexio.ConsultaAulesPanel(this, spaceManager, bookingManager, resourceManager,paymentManager);
        this.add(consultaAulesPanel, BorderLayout.CENTER);
        this.pack();
    }
    
    private void mnu_gestioreserves_consultaHistoricActionPerformed(ActionEvent evt) {
        desactivarPanels();
        historicDespesesPanel = new HistoricDespesesPanel(bookingManager);
        this.add(historicDespesesPanel, BorderLayout.CENTER);
        this.pack();
    }
    
    private void mnu_gestioreserves_consultaReservesActionPerformed(ActionEvent evt) {
        desactivarPanels();
        consultaReservesPanel = new ConsultaReservesPanel(bookingManager,spaceManager,paymentManager,userManager,resourceManager,this);
        this.add(consultaReservesPanel, BorderLayout.CENTER);
        this.pack();
    }
    
    private void mnu_tecncic_consultaAulesActionPerformed(ActionEvent evt) {
        desactivarPanels();
        consultaAulesPanelPartAules = new edu.uoc.tdp.pac4.only5.gui.client.panels.aules.ConsultaAulesPanel(bookingManager, spaceManager, userManager, resourceManager, paymentManager);
        this.add(consultaAulesPanelPartAules, BorderLayout.CENTER);
        this.pack();
    }
        
    private void mnu_administrador_novaAulaActionPerformed(ActionEvent evt) {
        desactivarPanels();
        aulaPanel = new AulaPanel(spaceManager);
        this.add(aulaPanel, BorderLayout.CENTER);
        this.pack();
    }
    
    private void mnu_administrador_nouUsuariActionPerformed(ActionEvent evt) {
        desactivarPanels();
        usuariPanel = new UsuariPanel(userManager);
        this.add(usuariPanel, BorderLayout.CENTER);
        this.pack();
    }
    
    private void mnu_administrador_consultaUsuarisActionPerformed(ActionEvent evt) {
        desactivarPanels();
        recursPanel = new RecursPanel(resourceManager);
        this.add(recursPanel, BorderLayout.CENTER);
        this.pack();
    }


    private void desactivarPanels() {
        for (Component component : this.getContentPane().getComponents()) {
            try {
                this.remove(component);
            } catch (Exception e) {
            }
        }
    }
}
